/**
 * $Id$
 *
 * @author Noritaka Horio<holy.shared.design@gmail.com>
 * @copyright 2009 Noritaka Horio all rights resrved. 
 * @license The MIT License
 * @version $Revision$
 * @since
 */


//Window enumerated type
var app = {
	"Window": {
		"CLOCK": 0,
		"HELP": 1
	}
};


var AppContext = new ApplicationContext();

//WindowManager Object
var WindowManager = new Hash();





var closeWindow = function(event) {
	var key = WindowManager.keyOf(event.target);
	if (key) {
		WindowManager.erase(key);
	}
}

var closeAllWindow = function(event) {
	var windows = air.NativeApplication.nativeApplication.openedWindows;
	for (var i = 0; l = windows.length, l > i; i++) {
		var w = windows[i];
		if (w.closed == false) {
			w.close();
		}
	}
}



window.addEvent("domready", function() {

	WindowManager.set(app.Window.CLOCK, nativeWindow);

	nativeWindow.addEventListener(air.Event.CLOSE, closeAllWindow);

	var navigation = $("warpper").getElement("ul.navigation");

	//最小化ボタンをクリックされた時の処理を定義
	var min = navigation.getElement("li a.minimum");
	min.addEvent("click", function() {
		nativeWindow.minimize();
	});

	//閉じるボタンをクリックされた時の処理を定義
	var close = navigation.getElement("li a.close");
	close.addEvent("click", function() {
		nativeWindow.close();
	});

	//オプションボタンをクリックされた時の処理を定義
	var option = navigation.getElement("li a.option");
	option.addEvent("click", function() {

		if (!WindowManager.has(app.Window.HELP)) {

			var op = new air.NativeWindowInitOptions();
			op.systemChrome = air.NativeWindowSystemChrome.STANDARD;
			op.type = air.NativeWindowType.NORMAL;
			op.transparent = false;
			op.maximizable = false;
			op.minimizable = false;
			op.resizable = false;
	
			var helpWindow = new air.NativeWindow(op);
			var htmlLoader = new air.HTMLLoader();
	
			htmlLoader.height = 400;
			htmlLoader.width = 300;
			helpWindow.height = 400;
			helpWindow.width = 300;
	
			htmlLoader.load(new air.URLRequest("help.html"))
			htmlLoader.visible = true;
	
			helpWindow.stage.align = "TL"; 
			helpWindow.stage.scaleMode = "noScale"; 
			helpWindow.stage.addChild(htmlLoader);
	
			helpWindow.title = AppContext.getName() + " - help";
	
			helpWindow.addEventListener(air.Event.CLOSING, closeWindow);
	
			helpWindow.activate();
			
			WindowManager.set(app.Window.HELP, helpWindow);
		} else {
			var win = WindowManager.get(app.Window.HELP);
			win.activate();
		}
	});

	//各ボタンのロールオーバー時の処理を定義
	var buttons = $(navigation).getElements("a");
	buttons.each(function(a, key) {
		a.addEvents({
			"mouseover": function() {
				var img = this.getElement("img");
				img.src = img.src.replace(/(\.gif|\.jpg|\.png)/, "_hover" + "$1");
			}.bind(a),
			"mouseout": function() {
				var img = this.getElement("img");
				img.src = img.src.replace("_hover", "");
			}.bind(a)
		});
	});

	$("warpper").addEvent("mousemove", function() {
		nativeWindow.startMove();
	});


	//実行するProcessingソースコード情報を収集する
	var loader = new PDE.ContextLoader({
		"onStart": function() {
			var title = new Element("p", {"class": "title"});
			var status = new Element("p", {"class": "status", "html": "loading"});
			var count = new Element("span", {"class": "count"});
 			var name = new Element("strong", {"html": ""});
			title.adopt([name, count]);

			$("clock").addClass("loading");
			$("clock").adopt([title, status]);
		}.bind(loader),

		"onProgress": function(count, pde) {
			var name	= $("clock").getElement("p.title strong");
			var loadCount	= $("clock").getElement("p.title span.count");
			name.set("html", pde.name);
			loadCount.set("html", count.toString());
		}.bind(loader),

		"onComplete": function(context) {
			PDEContext = context;

			$("clock").removeClass("loading");
			$("clock").innerHTML = "";

			//Clockオブジェクトのインスタンス生成
			var clock = new Clock("clock", Schedule, {
				"id": "processing",
				"swf": "app:/libraries/processing/processing.swf"
			});

			clock.addEvent("start", TimeLisner);
			clock.addEvent("start", MinuteLisner);
			clock.addEvent("time", MinuteLisner);
			clock.start();

		}.bind(loader)
	});
	loader.load();


});
